package com.example.user.service.impl;

import com.example.client.common.DictFeignClient;
import com.example.common.util.constant.Constants;
import com.example.common.util.constant.UserConstants;
import com.example.common.util.utils.StringUtils;
import com.example.model.domain.entity.user.Patient;
import com.example.user.mapper.PatientMapper;
import com.example.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author juzipi
 * @Date 2021/7/20 17:38
 * @Info
 */
@Service
public class PatientServiceImpl implements PatientService {


    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private DictFeignClient dictFeignClient;


    /**

     * 查询就诊人
     *
     * @param id 就诊人ID
     * @return 就诊人
     */
    @Override
    public Patient selectPatientById(Long id)
    {
        return this.patchPatient(patientMapper.selectPatientById(id));
    }

    /**

     * 查询就诊人列表
     *
     * @return 就诊人
     */
    @Override
    public List<Patient> selectPatientList()
    {
        List<Patient> patients = patientMapper.selectPatientList();
        //补充内容
        patients.forEach(this::patchPatient);
        return patients;
    }

    /**

     * 新增就诊人
     *
     * @param patient 就诊人
     * @return 结果
     */
    @Override
    public int insertPatient(Patient patient)
    {
        return patientMapper.insertPatient(patient);
    }

    /**

     * 修改就诊人
     *
     * @param patient 就诊人
     * @return 结果
     */
    @Override
    public int updatePatient(Patient patient)
    {
        return patientMapper.updatePatient(patient);
    }

    /**

     * 批量删除就诊人
     *
     * @param ids 需要删除的就诊人ID
     * @return 结果
     */
    @Override
    public int deletePatientByIds(Long[] ids)
    {
        return patientMapper.deletePatientByIds(ids);
    }


    /**
     * Patient对象里面其他参数封装
     * @param patient
     * @return
     */
    private Patient patchPatient(Patient patient) {
        if (StringUtils.isNotNull(patient)){
            //根据证件类型编码，获取证件类型具体指
            Object certificatesType = dictFeignClient.getName(UserConstants.CERTIFICATES_TYPE, patient.getCertificatesType()).get(Constants.MSG);//联系人证件
            //联系人证件类型
            Object contactsCertificatesType = dictFeignClient.getName(UserConstants.CERTIFICATES_TYPE,patient.getContactsCertificatesType()).get(Constants.MSG);
            //省
            Object province = dictFeignClient.getName(patient.getProvinceCode()).get(Constants.MSG);
            //市
            Object city = dictFeignClient.getName(patient.getCityCode()).get(Constants.MSG);
            //区
            Object district = dictFeignClient.getName(patient.getDistrictCode()).get(Constants.MSG);

            patient.getParam().put(UserConstants.CERTIFICATES_TYPE_STRING, certificatesType.toString());
            patient.getParam().put(UserConstants.CONTACTS_CERTIFICATES_TYPE_STRING, contactsCertificatesType.toString());
            patient.getParam().put(UserConstants.PROVINCE_STRING, province);
            patient.getParam().put(UserConstants.CITY_STRING, city);
            patient.getParam().put(UserConstants.DISTRICT_STRING, district);
            patient.getParam().put(UserConstants.FULL_ADDRESS, province.toString() + city + district + patient.getAddress());
            return patient;
        }
        return null;
    }

}
